package com.servlet.meeting;

import com.domain.MeetingRecord;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.MeetingService;
import com.service.impl.MettingServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/meetingSelectServlet")
public class MeetingSelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        MeetingService service = new MettingServiceImpl();
        ArrayList<MeetingRecord> mtt = service.select();
        if(mtt.size() < 1) {
            response.getWriter().write("{\"status\":\"0\",\"message\":\"ok\",\"data\":[]}");
            return;
        }
        ObjectMapper mapper = new ObjectMapper();
        StringBuilder data = new StringBuilder();
        for(MeetingRecord meetingRecord : mtt){
            String j = mapper.writeValueAsString(meetingRecord);
            data.append(j);
            data.append(",");
        }
        data.deleteCharAt(data.length()-1);
        response.getWriter().write("{\"status\":\"0\",\"message\":\"ok\",\"data\":["+data+"]}");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
